package JsonIgnore;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class User {
    private String username;

    @JsonIgnore // 忽略该字段
    private String password;

    private String email;

    public User() {}

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public static void main(String[] args) throws Exception {
        // 创建一个 User 对象
        User user = new User("码农研究僧", "secret123", "manong@example.com");

        // 创建 ObjectMapper 对象
        ObjectMapper mapper = new ObjectMapper();

        // 将 User 对象转换为 JSON 字符串
        String jsonString = mapper.writeValueAsString(user);
        System.out.println("序列化后的 JSON 字符串：" + jsonString);

        // 将 JSON 字符串反序列化为 User 对象
        String jsonInput = "{\"username\":\"码农研究僧\",\"password\":\"secret123\",\"email\":\"manong@example.com\"}";
        User deserializedUser = mapper.readValue(jsonInput, User.class);

        System.out.println("反序列化后的 User 对象：");
        System.out.println("Username: " + deserializedUser.getUsername());
        System.out.println("Password: " + deserializedUser.getPassword());
        System.out.println("Email: " + deserializedUser.getEmail());
    }
}
